package com.qf.controller;
import com.github.pagehelper.PageHelper;
import com.github.pagehelper.PageInfo;
import com.qf.bean.JsonResult;
import com.qf.pojo.GoodsDetail;
import com.qf.service.GoodsDetailService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestParam;
import org.springframework.web.bind.annotation.ResponseBody;
import org.springframework.web.multipart.MultipartFile;

import java.io.File;
import java.io.IOException;
import java.util.List;
import java.util.UUID;

@Controller
@RequestMapping("goods")
public class GoodsDetailController {

    @Autowired
    private GoodsDetailService goodsDetailService;

    private String uploadFileName1;
    private String uploadFileName2;
    private String uploadFileName3;
    private String uploadFileName4;
    private String uploadFileName5;
    private String uploadFileName6;

    @RequestMapping("findByPage")
    @ResponseBody
    public JsonResult findByPage(
            @RequestParam(value = "page",required = false,defaultValue = "1")Integer pageNum,
            @RequestParam(value = "limit",required = false,defaultValue = "2")Integer pageSize){

        PageHelper.startPage(pageNum,pageSize);

        List<GoodsDetail> goodsDetails = goodsDetailService.findAll();

        PageInfo<GoodsDetail> pageInfo = new PageInfo<>(goodsDetails);

        JsonResult jsonResult = JsonResult.ok();
        jsonResult.setCount(pageInfo.getTotal());
        jsonResult.setData(pageInfo.getList());

        return jsonResult;
    }

    @RequestMapping("deleteById")
    @ResponseBody
    public String deleteById(Integer goodId){
        System.out.println(goodId);
        goodsDetailService.deleteById(goodId);
        return "success";
    }

    @RequestMapping("deleteAll")
    @ResponseBody
    public String deleteAll( Integer[] goodId){
        goodsDetailService.deleteAll(goodId);
        return "success";
    }

//    @RequestMapping("findById")
//    public String findById(Integer goodsId, Model model){
//        System.out.println(goodsId+"====");
//        model.addAttribute("goodsId",goodsId);
//        return "/update.jsp";
//    }

    @RequestMapping("updateGoods")
    @ResponseBody
    public String updateGoods(GoodsDetail goodsDetail){
        goodsDetailService.updateGoods(goodsDetail);
        return "success";
    }

    @RequestMapping("upload1")
    @ResponseBody
    public JsonResult upload1(MultipartFile file){
        //上传的原文件名
        uploadFileName1 = file.getOriginalFilename();
        //使用UUID声明前缀，防止上传的图片名重复
        uploadFileName1 = UUID.randomUUID().toString().replace("-","")+uploadFileName1;
        //声明上传的路径
        String uploadPath = "F:\\QF\\apache-tomcat-8.5.31-windows-64位\\apache-tomcat-8.5.31\\webapps\\guimei\\";
        try {
            //上传
            file.transferTo(new File(uploadPath,uploadFileName1));
        } catch (IOException e) {
            e.printStackTrace();
        }
        JsonResult jsonResult = JsonResult.ok();
        jsonResult.setMsg("http://localhost:8088/guimei/"+uploadFileName1);
        return jsonResult;
    }
    @RequestMapping("upload2")
    @ResponseBody
    public JsonResult upload2(MultipartFile file){
        //上传的原文件名
        uploadFileName2 = file.getOriginalFilename();
        //使用UUID声明前缀，防止上传的图片名重复
        uploadFileName2 = UUID.randomUUID().toString().replace("-","")+uploadFileName2;
        //声明上传的路径
        String uploadPath = "F:\\QF\\apache-tomcat-8.5.31-windows-64位\\apache-tomcat-8.5.31\\webapps\\guimei\\";
        try {
            //上传
            file.transferTo(new File(uploadPath,uploadFileName2));
        } catch (IOException e) {
            e.printStackTrace();
        }
        JsonResult jsonResult = JsonResult.ok();
        jsonResult.setMsg("http://localhost:8088/guimei/"+uploadFileName2);
        return jsonResult;
    }
    @RequestMapping("upload3")
    @ResponseBody
    public JsonResult upload3(MultipartFile file){
        //上传的原文件名
        uploadFileName3 = file.getOriginalFilename();
        //使用UUID声明前缀，防止上传的图片名重复
        uploadFileName3 = UUID.randomUUID().toString().replace("-","")+uploadFileName3;
        //声明上传的路径
        String uploadPath = "F:\\QF\\apache-tomcat-8.5.31-windows-64位\\apache-tomcat-8.5.31\\webapps\\guimei\\";
        try {
            //上传
            file.transferTo(new File(uploadPath,uploadFileName3));
        } catch (IOException e) {
            e.printStackTrace();
        }
        JsonResult jsonResult = JsonResult.ok();
        jsonResult.setMsg("http://localhost:8088/guimei/"+uploadFileName3);
        return jsonResult;
    }
    @RequestMapping("upload4")
    @ResponseBody
    public JsonResult upload4(MultipartFile file){
        //上传的原文件名
        uploadFileName4 = file.getOriginalFilename();
        //使用UUID声明前缀，防止上传的图片名重复
        uploadFileName4 = UUID.randomUUID().toString().replace("-","")+uploadFileName4;
        //声明上传的路径
        String uploadPath = "F:\\QF\\apache-tomcat-8.5.31-windows-64位\\apache-tomcat-8.5.31\\webapps\\guimei\\";
        try {
            //上传
            file.transferTo(new File(uploadPath,uploadFileName4));
        } catch (IOException e) {
            e.printStackTrace();
        }
        JsonResult jsonResult = JsonResult.ok();
        jsonResult.setMsg("http://localhost:8088/guimei/"+uploadFileName4);
        return jsonResult;
    }
    @RequestMapping("upload5")
    @ResponseBody
    public JsonResult upload5(MultipartFile file){
        //上传的原文件名
        uploadFileName5 = file.getOriginalFilename();
        //使用UUID声明前缀，防止上传的图片名重复
        uploadFileName5 = UUID.randomUUID().toString().replace("-","")+uploadFileName5;
        //声明上传的路径
        String uploadPath = "F:\\QF\\apache-tomcat-8.5.31-windows-64位\\apache-tomcat-8.5.31\\webapps\\guimei\\";
        try {
            //上传
            file.transferTo(new File(uploadPath,uploadFileName5));
        } catch (IOException e) {
            e.printStackTrace();
        }
        JsonResult jsonResult = JsonResult.ok();
        jsonResult.setMsg("http://localhost:8088/guimei/"+uploadFileName5);
        return jsonResult;
    }
    @RequestMapping("upload6")
    @ResponseBody
    public JsonResult upload6(MultipartFile file){
        //上传的原文件名
        uploadFileName6 = file.getOriginalFilename();
        //使用UUID声明前缀，防止上传的图片名重复
        uploadFileName6 = UUID.randomUUID().toString().replace("-","")+uploadFileName6;
        //声明上传的路径
        String uploadPath = "F:\\QF\\apache-tomcat-8.5.31-windows-64位\\apache-tomcat-8.5.31\\webapps\\guimei\\";
        try {
            //上传
            file.transferTo(new File(uploadPath,uploadFileName6));
        } catch (IOException e) {
            e.printStackTrace();
        }
        JsonResult jsonResult = JsonResult.ok();
        jsonResult.setMsg("http://localhost:8088/guimei/"+uploadFileName6);
        return jsonResult;
    }

    @RequestMapping("addGoods")
    @ResponseBody
    public String addGoods(GoodsDetail goodsDetail){
        //设置用户图片地址
        goodsDetail.setBigPic("http://localhost:8088/guimei/"+uploadFileName1);
        goodsDetail.setPic("http://localhost:8088/guimei/"+uploadFileName2);
        goodsDetail.setSmallPic1("http://localhost:8088/guimei/"+uploadFileName3);
        goodsDetail.setSmallPic2("http://localhost:8088/guimei/"+uploadFileName4);
        goodsDetail.setSmallPic3("http://localhost:8088/guimei/"+uploadFileName5);
        goodsDetail.setSmallPic4("http://localhost:8088/guimei/"+uploadFileName6);
        System.out.println(goodsDetail);
        goodsDetailService.addGoods(goodsDetail);
        return "success";
    }


}
